package src.queries;

import java.sql.Connection;
import java.util.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserQueries extends UnsecuredQueries{

	public UserQueries(Connection c, SecurityQueries security) {
		super(c, security);
	}
	
	public void updatePassword(String userName, String newPassword) throws SQLException{
		PreparedStatement p = makePreparedStatement(
				"UPDATE users SET password = ?" +
				"WHERE user_name = ?" );

		p.setString(1, newPassword);
		p.setString(2, userName);
		p.executeUpdate();
		
		PreparedStatement pcom = makePreparedStatement("commit");
		pcom.executeUpdate();
	}
	public void updateUserClass(String username, String newClass) throws SQLException{
		PreparedStatement p = makePreparedStatement(
				"UPDATE users SET class = ?" +
				"WHERE user_name = ?" );

		p.setString(1, newClass);
		p.setString(2, username);
		p.executeUpdate();
		
		PreparedStatement pcom = makePreparedStatement("commit");
		pcom.executeUpdate();
	}
	public void updateDateRegistered(String username, Date dateRegistered) throws SQLException{
		PreparedStatement p = makePreparedStatement(
				"UPDATE users SET date_registered = ?" +
				"WHERE user_name = ?" );

		p.setDate(1, parseDate(dateRegistered));
		p.setString(2, username);
		p.executeUpdate();
		
		PreparedStatement pcom = makePreparedStatement("commit");
		pcom.executeUpdate();
	}
	public void updateFirstName(String username, String newFirstName) throws SQLException{
		PreparedStatement p = makePreparedStatement(
				"UPDATE persons SET first_name = ?" +
				"WHERE user_name = ?" );

		p.setString(1, newFirstName);
		p.setString(2, username);
		p.executeUpdate();
		
		PreparedStatement pcom = makePreparedStatement("commit");
		pcom.executeUpdate();
	}
	public void updateLastName(String username, String newLastName) throws SQLException{
		PreparedStatement p = makePreparedStatement(
				"UPDATE persons SET last_name = ?" +
				"WHERE user_name = ?" );

		p.setString(1, newLastName);
		p.setString(2, username);
		p.executeUpdate();
		
		PreparedStatement pcom = makePreparedStatement("commit");
		pcom.executeUpdate();
	}
	public void updateAddress(String username, String newAddress) throws SQLException{
		PreparedStatement p = makePreparedStatement(
				"UPDATE persons SET address = ?" +
				"WHERE user_name = ?" );

		p.setString(1, newAddress);
		p.setString(2, username);
		p.executeUpdate();
		
		PreparedStatement pcom = makePreparedStatement("commit");
		pcom.executeUpdate();
	}
	public void updateEmail(String username, String newEmail) throws SQLException{
		PreparedStatement p = makePreparedStatement(
				"UPDATE persons SET email = ?" +
				"WHERE user_name = ?" );

		p.setString(1, newEmail);
		p.setString(2, username);
		p.executeUpdate();
		
		PreparedStatement pcom = makePreparedStatement("commit");
		pcom.executeUpdate();
	}
	public void updatePhone(String username, String newPhone) throws SQLException{
		PreparedStatement p = makePreparedStatement(
				"UPDATE persons SET phone = ?" +
				"WHERE user_name = ?" );

		p.setString(1, newPhone);
		p.setString(2, username);
		p.executeUpdate();
		
		PreparedStatement pcom = makePreparedStatement("commit");
		pcom.executeUpdate();
	}
}
